package com.pku.control;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import java.util.List;

public class FragmentUpdateAdapter extends OpenPagerAdapter<Fragment> {
    private List<String> titles;
    private List<Fragment> fragments;

    public FragmentUpdateAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) {
        super(fm);
        this.fragments = fragments;
        this.titles = titles;
    }

    @Override
    public Fragment getItem(int position) {
        return fragments == null ? null : fragments.get(position);
    }

    @Override
    protected Fragment getItemData(int position) {
        return position<fragments.size()?fragments.get(position):null;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles == null ? null : titles.get(position);
    }

    @Override
    protected boolean dataEquals(Fragment oldData, Fragment newData) {
        return oldData.equals(newData);
    }

    @Override
    protected int getDataPosition(Fragment data) {
        return fragments.indexOf(data);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }
    public void setTitles(List<String> titles){
        this.titles=titles;
    }
}
